// SimpleAutoTask.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <locale.h>
#include "TravianAutoTask.h"
#include "MainFrm.h"
#include "FormTask.h"
#include "IniReader.h"
#include "AboutDlg.h"

BEGIN_MESSAGE_MAP(CAutoTaskApp, CWinApp)
	ON_COMMAND(ID_APP_ABOUT, &CAutoTaskApp::OnAppAbout)
END_MESSAGE_MAP()


// CYahooApp construction

CAutoTaskApp::CAutoTaskApp()
	: m_uElapseFailedTask(10*60)	// 10 min
{
	CIniReader	iniFile(_T(".\\user.ini"));
	m_TribeType = E_ROMANS;
	m_wLanguageID = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
	m_uRandomAchievement = iniFile.ReadInteger("setting","RandomAchievement",0);
#ifdef _DEBUG
	m_uElapseTask		= iniFile.ReadInteger("setting","ElapseTask",1);
#else
	m_uElapseTask		= iniFile.ReadInteger("setting","ElapseTask",600);
#endif
	m_dwTickFreq		= iniFile.ReadInteger("setting","TickFrequency",5000);		// 5000 msec = 5 sec
	m_bUseMutex			= iniFile.ReadInteger("setting","UseMutex",1);

	if( m_uRandomAchievement )
		srand( (unsigned)time( NULL ) );

	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

const char	*CAutoTaskApp::GetTroopTypeStr(UINT ind) const
{
	int	nID = IDS_STRING60011+(m_TribeType-1)*(TRAVIAN_TROOPTYPEID_UPLIMIT-TRAVIAN_TROOPTYPEID_LOWLIMIT+1+1);

	if ( m_TribeType == E_GAULS && ind == TRAVIAN_TRAP_ID )	// trap
		nID += TRAVIAN_TROOPTYPEID_UPLIMIT;
	else if( ind >= TRAVIAN_TROOPTYPEID_LOWLIMIT && ind <= TRAVIAN_TROOPTYPEID_UPLIMIT )
		nID += ind-1;
	else if( ind == TRAVIAN_ALL_TROOPTYPE )
		nID = IDS_ALL_TROOPTYPE;
	else
		return "?????";

	static CStringT< TCHAR, StrTraitMFC< TCHAR > >	cs;
	cs.LoadString(AfxGetInstanceHandle(),nID,m_wLanguageID);
	return cs;
}

const char	*CAutoTaskApp::GetDispatchTypeStr(UINT ind) const
{
	static CStringT< TCHAR, StrTraitMFC< TCHAR > >	cs;
	cs.LoadString(AfxGetInstanceHandle(),IDS_STRING60050+ind-2,m_wLanguageID);
	return cs;
}

const char	*CAutoTaskApp::LoadString(UINT ind) const
{
	static CStringT< TCHAR, StrTraitMFC< TCHAR > >	cs;
	cs.LoadString(AfxGetInstanceHandle(),ind,m_wLanguageID);
	return cs;
}


// The one and only CYahooApp object

CAutoTaskApp	theApp;
CEventLog		g_TravianEvtLog(NULL, _T("TravianAutoTask"));
COLORREF		g_crBkgnd(RGB(0xFF, 0xFB, 0xF0));

// CYahooApp initialization

BOOL CAutoTaskApp::InitInstance()
{
	// enable memory leak accurate dump
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

	InitCommonControls();

	CWinApp::InitInstance();
	AfxEnableControlContainer();
	

	if (!AfxOleInit())
	{
		AfxMessageBox(_T("Fatal Error: OLE initialization failed\n"), (MB_OK|MB_ICONSTOP));
		return FALSE;
		// TODO: code your application's behavior here.
	}

	// This will load RichEdit 2.0 DLL.
	AfxInitRichEdit2 ();


	CMainFrm* pMainFrame = new CMainFrm;
	if (! ((CMainFrm*)pMainFrame)->LoadFrame(IDR_MAIN,
		WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL) )
	{
		// free the memory
		delete pMainFrame;
		return FALSE;
	}

	m_mXP.LoadMenu(IDR_MAIN);
	m_mXP.LoadToolbar(IDR_TOOLBAR256);
	m_mXP.SetIconSize(16,16);
	pMainFrame->SetMenu(&m_mXP);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	Tasks* pts = new Tasks();
	if( cmdInfo.m_nShellCommand == CCommandLineInfo::FileOpen ) {
		if (! CTravianTask::GetTasks(cmdInfo.m_strFileName, *pts, g_TravianEvtLog) )
		{
			delete pts;
			pts = NULL;
		}
	}

	pMainFrame->ShowWindow(SW_MAXIMIZE);
	pMainFrame->UpdateWindow();
	m_pMainWnd = pMainFrame;
	pMainFrame->Init(pts);

	return TRUE;
}

void CAutoTaskApp::OnAppAbout()
{
	// TODO: Add your command handler code here
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}

void CAutoTaskApp::ReadLanguageID(const char *sLang)
{
	if (_stricmp(sLang,"hun") == 0)
	{
		m_wLanguageID = MAKELANGID(LANG_HUNGARIAN, SUBLANG_DEFAULT);
	} 
	else if (_stricmp(sLang,"eng") == 0)
	{
		m_wLanguageID = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
	}
	setlocale(LC_ALL,sLang);
}
